package com.sd.activity;

import java.io.File;
import java.util.Vector;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AutoFilesActivity extends ListActivity {

	private final static String TAG = "AutoFilesActivity";
	private Vector<File> mFiles;
	private String mDir = "/sdcard";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = this.getIntent();
		Bundle bl = intent.getExtras();
		String title = bl.getString("explorer_title");
		Uri uri = intent.getData();
		mDir = uri.getPath();

		setTitle(title);
		getData();
		MyAdapter adapter = new MyAdapter(this);
		setListAdapter(adapter);

		WindowManager m = getWindowManager();
		Display d = m.getDefaultDisplay();
		LayoutParams p = getWindow().getAttributes();
	}

	private void getData() {

		mFiles = new Vector<File>();
		File f=new File("/sdcard/");
		getFileList(f);
	}

	private void getFileList(File file) {
		// 列出指定路径下所有文件
		File[] files = file.listFiles();

		// 遍历当前各个文件
		if(files == null)
			return ;
		
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			if (f.isFile()) {
				// 如果是文件，则检查其扩展名是否为想要的图片类型
				if (getImageFile(f.getPath())) {
					// 是，则添加进全局量
					mFiles.add(f);
					Log.d(TAG, "name:"+f.getName());
				}
			} else if (f.isDirectory()) {

				// 不是文件，而是文件夹，进一步检查这个文件夹下面文件
				getFileList(f);
			}
		}
	}

	private boolean getImageFile(String path){
		if(path.endsWith("txt")
			|| path.endsWith("umd")
			|| path.endsWith("epub")){
			return true;
		}else{
			return false;
		}
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		File file = mFiles.get(position);
		finishWithResult(file.getPath());
	}

	public final class ViewHolder {
		public ImageView img;
		public TextView title;
		public TextView info;
	}

	public class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return mFiles.size();
		}

		public Object getItem(int arg0) {
			return mFiles.get(arg0);
		}

		public long getItemId(int arg0) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.listview, null);
				holder.img = (ImageView) convertView.findViewById(R.id.img);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.info = (TextView) convertView.findViewById(R.id.info);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			File file = mFiles.get(position);
			holder.title.setText(file.getName());
			holder.info.setText(file.getPath());
			return convertView;
		}
	}

	private void finishWithResult(String path) {
		Bundle conData = new Bundle();
		conData.putString("results", "Thanks Thanks");
		Intent intent = new Intent();
		intent.putExtras(conData);
		Uri startDir = Uri.fromFile(new File(path));
		intent.setDataAndType(startDir,
				"vnd.android.cursor.dir/lysesoft.andexplorer.file");
		setResult(RESULT_OK, intent);
		finish();
	}
};
